
interface IDataSource<T> {
  totalCount(): number;
  getData(index: number): T;
  addData(index: number, data: T): void;
  pushData(data: T): void;
  clearData(): void;
}

// 实现基础数据源类
class ArrayDataSource<T> implements IDataSource<T> {
  private dataList: Array<T> = [];

  totalCount(): number {
    return this.dataList.length;
  }

  getData(index: number): T {
    return this.dataList[index];
  }

  addData(index: number, data: T): void {
    this.dataList.splice(index, 0, data);
  }

  pushData(data: T): void {
    this.dataList.push(data);
  }

  clearData(): void {
    this.dataList = [];
  }
}

// 集合管理类
export class CollectionManager {
  private allData: number[] = []; // 全部数据
  private dataSource: IDataSource<number>; // 数据存储与监听
  private groupSize: number; // 每组数据大小
  private loadedIndices: Set<number> = new Set(); // 已加载的索引
  private loadedGroups: Map<number, number[]> = new Map();

  constructor(data: number[], groupSize: number) {
    this.allData = data;
    this.groupSize = groupSize;
    this.dataSource = new ArrayDataSource<number>();
  }

  // 加载指定索引范围的数据
  private loadRange(start: number, end: number, externalData?: number[]): void {
    for (let i = start; i <= end; i++) {
      if (!this.loadedIndices.has(i) && this.allData.includes(i)) {
        const data = externalData?.find((item) => item === this.allData[i]) || this.allData[i];
        this.dataSource.addData(i, data);
        this.loadedIndices.add(i);
      }
    }
  }
  // 加载指定分组的数据
  public loadGroup(groupIndex: number): number[] {
    if (this.loadedGroups.has(groupIndex)) {
      return this.loadedGroups.get(groupIndex) || [];
    }

    const start = groupIndex * this.groupSize;
    const end = Math.min(start + this.groupSize, this.allData.length);
    const groupData = this.allData.slice(start, end);
    this.loadedGroups.set(groupIndex, groupData);
    console.log(`Loaded group [${start}-${end - 1}]:`, groupData);
    return groupData;
  }

  // 获取指定索引所在的分组
  public getGroupIndex(index: number): number {
    return Math.floor(index / this.groupSize);
  }

  // 清除已加载数据
  public clear(): void {
    this.loadedGroups.clear();
  }

  // 根据全局索引加载当前分组数据
  public loadCurrentGroup(index: number, externalData?: number[]): void {
    const start = Math.floor(index / this.groupSize) * this.groupSize;
    const end = Math.min(start + this.groupSize - 1, this.allData.length - 1);
    this.loadRange(start, end, externalData);

    console.log(`Loaded group: [${start}-${end}]`);
  }

  // 滑动时加载新的数据段
  public handleSwipe(index: number, externalData?: number[]): void {
    const start = Math.floor(index / this.groupSize) * this.groupSize;
    const end = Math.min(start + this.groupSize - 1, this.allData.length - 1);

    // if (!this.isGroupLoaded(start, end)) {
    //   this.loadRange(start, end, externalData);
    // }
  }

  // 判断当前范围数据是否已经加载
  public isGroupLoaded(groupIndex: number): boolean {
    return this.loadedGroups.has(groupIndex);
  }

  // 获取当前分组数据
  public getCurrentGroup(): number[] {
    return Array.from(this.loadedIndices)
      .sort((a, b) => a - b)
      .map((index) => this.dataSource.getData(index));
  }

  // 添加新的数据
  public addData(index: number, data: number): void {
    if (!this.loadedIndices.has(index)) {
      this.dataSource.addData(index, data);
      this.loadedIndices.add(index);
    }
  }
}